<html>
 <head>
  <meta charset="utf-8"/>
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"/>
  <title>
   用 Python 理解服务器模型(上)  | 数螺 | NAUT IDEA
  </title>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" rel="stylesheet"/>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
  <style type="text/css">
   #xmain img {
                  max-width: 100%;
                  display: block;
                  margin-top: 10px;
                  margin-bottom: 10px;
                }

                #xmain p {
                    line-height:150%;
                    font-size: 16px;
                    margin-top: 20px;
                }

                #xmain h2 {
                    font-size: 24px;
                }

                #xmain h3 {
                    font-size: 20px;
                }

                #xmain h4 {
                    font-size: 18px;
                }


                .header {
	           background-color: #0099ff;
	           color: #ffffff;
	           margin-bottom: 20px;
	        }

	        .header p {
                  margin: 0px;
                  padding: 10px 0;
                  display: inline-block;  
                  vertical-align: middle;
                  font-size: 16px;
               }

               .header a {
                 color: white;
               }

              .header img {
                 height: 25px;
              }
  </style>
  <script src="http://cdn.bootcss.com/jquery/3.0.0/jquery.min.js">
  </script>
  <script src="http://nautstatic-10007657.file.myqcloud.com/static/css/readability.min.js" type="text/javascript">
  </script>
  <script type="text/javascript">
   $(document).ready(function() {
                 var loc = document.location;
                 var uri = {
                  spec: "http://dataunion.org/22882.html",
                  host: "http://dataunion.org",
                  prePath: "http://dataunion.org",
                  scheme: "http",
                  pathBase: "http://dataunion.org/"
                 };
    
                 var documentClone = document.cloneNode(true);
                 var article = new Readability(uri, documentClone).parse();
     
                 document.getElementById("xmain").innerHTML = article.content;
                });
  </script>
  <!-- 1466460693: Accept with keywords: (title(0.4):Python,服务器,模型,数盟,社区, topn(0.3):社区,操作系统,发送数据,数盟,行业资讯,事件,数组,数据挖掘,数量,变量,遍历,Python,例子,参数,文章,服务器,数据,函数,网卡,汽车,进程,客户端,问题,编程语言,错误,代码,线程,内核,版本,时候).-->
 </head>
 <body onload="">
  <div class="header">
   <div class="container">
    <div class="row">
     <div class="col-xs-6 col-sm-6 text-left">
      <a href="/databee">
       <img src="http://nautidea-10007657.cos.myqcloud.com/logo_white.png"/>
      </a>
      <a href="/databee">
       <p>
        数螺
       </p>
      </a>
     </div>
     <div class="hidden-xs col-sm-6 text-right">
      <p>
       致力于数据科学的推广和知识传播
      </p>
     </div>
    </div>
   </div>
  </div>
  <div class="container text-center">
   <h1>
    用 Python 理解服务器模型(上)
   </h1>
  </div>
  <div class="container" id="xmain">
   ﻿﻿
   <title>
    用 Python 理解服务器模型(上) | 数盟社区
   </title>
   <!-- All in One SEO Pack 2.2.7.6.2 by Michael Torbert of Semper Fi Web Design[32,66] -->
   <!-- /all in one seo pack -->
   <!--
<div align="center">
<a href="http://strata.oreilly.com.cn/hadoop-big-data-cn?cmp=mp-data-confreg-home-stcn16_dataunion_pc" target="_blank"><img src="http://dataunion.org/wp-content/uploads/2016/05/stratabj.jpg"/ ></a>
</div>
-->
   <header id="header-web">
    <div class="header-main">
     <hgroup class="logo">
      <h1>
       <a href="http://dataunion.org/" rel="home" title="数盟社区">
        <img src="http://dataunion.org/wp-content/themes/yzipi/images/logo.png"/>
       </a>
      </h1>
     </hgroup>
     <!--logo-->
     <nav class="header-nav">
      <ul class="menu" id="menu-%e4%b8%bb%e8%8f%9c%e5%8d%95">
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-71" id="menu-item-71">
        <a href="http://dataunion.org/category/events" title="events">
         活动
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22457" id="menu-item-22457">
          <a href="http://dataunion.org/2016timeline">
           2016档期
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-22459" id="menu-item-22459">
          <a href="http://dataunion.org/category/parterc">
           合作会议
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor menu-item-has-children menu-item-20869" id="menu-item-20869">
        <a href="http://dataunion.org/category/tech" title="articles">
         文章
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20867" id="menu-item-20867">
          <a href="http://dataunion.org/category/tech/base" title="base">
           基础架构
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3302" id="menu-item-3302">
          <a href="http://dataunion.org/category/tech/ai" title="ai">
           人工智能
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3303" id="menu-item-3303">
          <a href="http://dataunion.org/category/tech/analysis" title="analysis">
           数据分析
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21920" id="menu-item-21920">
          <a href="http://dataunion.org/category/tech/dm">
           数据挖掘
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3314" id="menu-item-3314">
          <a href="http://dataunion.org/category/tech/viz" title="viz">
           可视化
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor current-menu-parent current-post-parent menu-item-3305" id="menu-item-3305">
          <a href="http://dataunion.org/category/tech/devl" title="devl">
           编程语言
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-20876" id="menu-item-20876">
        <a href="http://dataunion.org/category/industry">
         行业
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-16328" id="menu-item-16328">
          <a href="http://dataunion.org/category/industry/case" title="case">
           行业应用
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-2112" id="menu-item-2112">
          <a href="http://dataunion.org/category/industry/demo" title="demo">
           Demo展示
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21562" id="menu-item-21562">
          <a href="http://dataunion.org/category/industry/news">
           行业资讯
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-311" id="menu-item-311">
        <a href="http://dataunion.org/category/sources" title="sources">
         资源
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20870" id="menu-item-20870">
        <a href="http://dataunion.org/category/books" title="book">
         图书
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21363" id="menu-item-21363">
        <a href="http://dataunion.org/category/training">
         课程
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-21853" id="menu-item-21853">
        <a href="http://dataunion.org/category/jobs">
         职位
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-22050" id="menu-item-22050">
          <a href="http://dataunion.org/category/career">
           职业规划
          </a>
         </li>
        </ul>
       </li>
      </ul>
     </nav>
     <!--header-nav-->
    </div>
   </header>
   <!--header-web-->
   <div id="main">
    <div id="soutab">
     <form action="http://dataunion.org/" class="search" method="get">
     </form>
    </div>
    <div id="container">
     <nav id="mbx">
      当前位置：
      <a href="http://dataunion.org">
       首页
      </a>
      &gt;
      <a href="http://dataunion.org/category/tech">
       文章
      </a>
      &gt;
      <a href="http://dataunion.org/category/tech/devl">
       编程语言
      </a>
      &gt;  正文
     </nav>
     <!--mbx-->
     <article class="content">
      <header align="centre" class="contenttitle">
       <div class="mscc">
        <h1 class="mscctitle">
         <a href="http://dataunion.org/22882.html">
          用 Python 理解服务器模型(上)
         </a>
        </h1>
        <address class="msccaddress ">
         <em>
          770 次阅读 -
         </em>
         <a href="http://dataunion.org/category/tech/devl" rel="category tag">
          编程语言
         </a>
        </address>
       </div>
      </header>
      <div class="content-text">
       <p>
        原文链接
        <a href="https://codingstyle.cn/topics/134?hmsr=toutiao.io&amp;utm_medium=toutiao.io&amp;utm_source=toutiao.io">
         https://codingstyle.cn/topics/134?hmsr=toutiao.io&amp;utm_medium=toutiao.io&amp;utm_source=toutiao.io
        </a>
       </p>
       <h2>
        0. 前言
       </h2>
       <p>
        没有前言。
       </p>
       <h2>
        1. 一个简单的开始
       </h2>
       <p>
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a0bc3860100704052" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <span class="crayon-language">
           Python
          </span>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          import socket

response = 'HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-Length: 1\r\n\r\nA'

server = socket.socket()
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(('0.0.0.0', 8080))
server.listen(32)

while True:
        client, clientaddr = server.accept()  # blocking
        request = client.recv(4096)  # blocking
        client.send(response)  # maybe blocking
        client.close()
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57686a0bc3860100704052-1">
               1
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3860100704052-2">
               2
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc3860100704052-3">
               3
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3860100704052-4">
               4
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc3860100704052-5">
               5
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3860100704052-6">
               6
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc3860100704052-7">
               7
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3860100704052-8">
               8
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc3860100704052-9">
               9
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3860100704052-10">
               10
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc3860100704052-11">
               11
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3860100704052-12">
               12
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc3860100704052-13">
               13
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3860100704052-14">
               14
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57686a0bc3860100704052-1">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3860100704052-2">
              </div>
              <div class="crayon-line" id="crayon-57686a0bc3860100704052-3">
               <span class="crayon-v">
                response
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-s">
                'HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-Length: 1\r\n\r\nA'
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3860100704052-4">
              </div>
              <div class="crayon-line" id="crayon-57686a0bc3860100704052-5">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3860100704052-6">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                setsockopt
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                SOL_SOCKET
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                SO_REUSEADDR
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc3860100704052-7">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                bind
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                '0.0.0.0'
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                8080
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3860100704052-8">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                listen
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                32
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc3860100704052-9">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3860100704052-10">
               <span class="crayon-st">
                while
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                True
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc3860100704052-11">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                clientaddr
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                accept
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-c">
                # blocking
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3860100704052-12">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                request
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                recv
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                4096
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-c">
                # blocking
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc3860100704052-13">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                send
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                response
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-c">
                # maybe blocking
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3860100704052-14">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                close
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0127 seconds] -->
       <p>
       </p>
       <p>
        这可能是我写过最简单的服务器了。
       </p>
       <p>
        解释一下程序中的变量。
       </p>
       <p>
        <code>
         response
        </code>
        服务器的返回。
       </p>
       <p>
        作为示例，我使用了一个简单的
        <code>
         HTTP
        </code>
        响应作为服务器的回复，返回一个字母 ‘A’ ，这样我就可以使用各种
        <code>
         HTTP
        </code>
        的工具来检测服务器的性能了（比如
        <code>
         ab
        </code>
        和
        <code>
         wrk
        </code>
        ，后面的例子我会用
        <code>
         wrk
        </code>
        ）。
       </p>
       <p>
        <code>
         server
        </code>
        服务器的 socket 对象。
       </p>
       <p>
        <code>
         client
        </code>
        客户端的 socket 对象。
       </p>
       <p>
        <code>
         clientaddr
        </code>
        客户端的地址，以
        <code>
         (ip,port)
        </code>
        形式由
        <code>
         accept
        </code>
        创建。
       </p>
       <p>
        <code>
         request
        </code>
        客户端发来的请求数据。
       </p>
       <p>
        每章开始的完整示例代码都可以直接运行，没有依赖，但是否能运行在 Python 3 上我还没有测试过。欢迎测试过的朋友告诉我。
       </p>
       <h3>
        1.1 Socket 插口
       </h3>
       <p>
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a0bc3873923270436" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <span class="crayon-language">
           Python
          </span>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          server = socket.socket()
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57686a0bc3873923270436-1">
               1
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57686a0bc3873923270436-1">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0069 seconds] -->
       <p>
       </p>
       <p>
        创建
        <code>
         server
        </code>
        的
        <code>
         socket
        </code>
        ，如果在
        <code>
         C
        </code>
        语言中，
        <code>
         socket()
        </code>
        函数应该是返回一个
        <code>
         int
        </code>
        类型的文件描述符（
        <code>
         File Descriptor
        </code>
        简称
        <code>
         fd
        </code>
        ），在
        <code>
         Windows
        </code>
        下通常也叫做名柄。
       </p>
       <p>
        在
        <code>
         Python
        </code>
        中，是由
        <code>
         socket
        </code>
        库封装的一个
        <code>
         socket
        </code>
        对象，如果想获取原始的文件描述符，可以调用函数
        <code>
         server.fileno()
        </code>
        ，它会返回
        <code>
         int
        </code>
        类型的
        <code>
         fd
        </code>
        。
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a0bc387a088741281" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <span class="crayon-language">
           Python
          </span>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57686a0bc387a088741281-1">
               1
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57686a0bc387a088741281-1">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                setsockopt
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                SOL_SOCKET
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                SO_REUSEADDR
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0012 seconds] -->
       <p>
       </p>
       <p>
        这行代码的作用是保证在
        <code>
         TIME_WAIT
        </code>
        状态下 bind() 调用可以成功，能够使服务器退出之后，可以被立即重启。你可以自己实验一下去掉这行代码之后，服务器重启的表现。更多关于
        <code>
         TIME_WAIT
        </code>
        的内容可以自己 Google 一下。如果比较懒那只要记得在每次创建服务器 socket 的时候都要把 SO_REUSEADDR 参数设成 1 就行了。
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a0bc3881742662830" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <span class="crayon-language">
           Python
          </span>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          server.bind(('0.0.0.0', 8080))
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57686a0bc3881742662830-1">
               1
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57686a0bc3881742662830-1">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                bind
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                '0.0.0.0'
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                8080
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0009 seconds] -->
       <p>
       </p>
       <p>
        绑定到本机的IP和端口，0.0.0.0 代表本机所有的 IPv4 的 IP 。
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a0bc3887620207135" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <span class="crayon-language">
           Python
          </span>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          server.listen(32)
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57686a0bc3887620207135-1">
               1
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57686a0bc3887620207135-1">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                listen
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                32
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0008 seconds] -->
       <p>
       </p>
       <p>
        listen 创建一个接收连接的队列，参数 32 是允许等待的连接的数量。
       </p>
       <p>
        <code>
         while True
        </code>
        的循环体是服务器的处理逻辑，也是这篇文章的核心内容。这里只是简单的接收客户端连接（
        <code>
         accept
        </code>
        调用） 和客户端数据（
        <code>
         recv
        </code>
        调用）并发送回复 （
        <code>
         send
        </code>
        调用）。最后关闭连接 （
        <code>
         close
        </code>
        调用）。出于易于理解的目的，我没有写任何错误处理。
       </p>
       <p>
        <code>
         client
        </code>
        变量和
        <code>
         server
        </code>
        变量一样，在
        <code>
         C
        </code>
        中是一个
        <code>
         fd
        </code>
        ，在
        <code>
         Python
        </code>
        中是一个
        <code>
         socket
        </code>
        的对象。
       </p>
       <h3>
        1.2 Blocking 阻塞
       </h3>
       <p>
        我在三处会发生阻塞的地方加了注释。
       </p>
       <p>
        第一处阻塞
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a0bc388e653471801" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <span class="crayon-language">
           Python
          </span>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          client, clientaddr = server.accept()  # blocking
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57686a0bc388e653471801-1">
               1
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57686a0bc388e653471801-1">
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                clientaddr
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                accept
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-c">
                # blocking
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0010 seconds] -->
       <p>
       </p>
       <p>
        当调用的函数是一个阻塞的系统调用时，如果没有满足的数据，这个系统调用不会返回，进程会被操作系统挂起，直到有满足的数据才会将进程唤醒。比如
        <code>
         accept
        </code>
        在此处就会发生阻塞。
       </p>
       <p>
        第二处阻塞
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a0bc3896461926537" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <span class="crayon-language">
           Python
          </span>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          request = client.recv(4096)  # blocking
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57686a0bc3896461926537-1">
               1
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57686a0bc3896461926537-1">
               <span class="crayon-v">
                request
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                recv
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                4096
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-c">
                # blocking
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0010 seconds] -->
       <p>
       </p>
       <p>
        <code>
         recv
        </code>
        和
        <code>
         accept
        </code>
        一样。
       </p>
       <p>
        第三处可能的阻塞
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a0bc38a2756263679" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <span class="crayon-language">
           Python
          </span>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          client.send(response)  # maybe blocking
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57686a0bc38a2756263679-1">
               1
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57686a0bc38a2756263679-1">
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                send
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                response
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-c">
                # maybe blocking
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0008 seconds] -->
       <p>
       </p>
       <p>
        <code>
         send
        </code>
        调用是
        <strong>
         可能阻塞
        </strong>
        。
       </p>
       <p>
        在内核的
        <code>
         socket
        </code>
        实现中，会有两个缓存 (buffer)。read buffer 和 write buffer 。当内核接收到网卡传来的客户端从数据后，把数据复制到 read buffer ，这个时候
        <code>
         recv
        </code>
        阻塞的进程就可以被唤醒。
       </p>
       <p>
        当调用
        <code>
         send
        </code>
        的时候，内核只是把
        <code>
         send
        </code>
        的数据复制到 write buffer 里，然后立即返回。只有 write buffer 的空间不够时
        <code>
         send
        </code>
        才会被阻塞，需要等待网卡发送数据腾空 write buffer 。在 write buffer 的空间足够放下
        <code>
         send
        </code>
        的数据时进程才可以被唤醒。
       </p>
       <p>
        这个例子是阻塞的，在
        <code>
         accept
        </code>
        阻塞的时候，无法调用
        <code>
         recv
        </code>
        接收客户端数据。在
        <code>
         recv
        </code>
        阻塞的时候也无法调用
        <code>
         accept
        </code>
        接受新的请求。所以它同时只能接受和处理一个请求，如果一个客户端发送数据的速度比较慢，会拖慢整个服务器响应的速度。
       </p>
       <p>
        但在局域网的情况下却很快，我用 wrk 测试一下这个简单 server 的性能。
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a0bc38ad570560215" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <span class="crayon-language">
           Python
          </span>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          $ wrk http://127.0.0.1:8080
Running 10s test @ http://127.0.0.1:8080
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     7.36ms   37.23ms 359.84ms   96.43%
    Req/Sec     2.98k     1.03k    3.69k    84.91%
  16293 requests in 10.08s, 0.90MB read
Requests/sec:   1616.60
Transfer/sec:     91.57KB
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57686a0bc38ad570560215-1">
               1
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ad570560215-2">
               2
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ad570560215-3">
               3
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ad570560215-4">
               4
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ad570560215-5">
               5
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ad570560215-6">
               6
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ad570560215-7">
               7
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ad570560215-8">
               8
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ad570560215-9">
               9
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57686a0bc38ad570560215-1">
               <span class="crayon-sy">
                $
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                wrk
               </span>
               <span class="crayon-v">
                http
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-cn">
                127.0.0.1
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-cn">
                8080
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ad570560215-2">
               <span class="crayon-i">
                Running
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                10s
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                test
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-sy">
                @
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                http
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-cn">
                127.0.0.1
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-cn">
                8080
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ad570560215-3">
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                2
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                threads
               </span>
               <span class="crayon-st">
                and
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                10
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                connections
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ad570560215-4">
               <span class="crayon-e">
               </span>
               <span class="crayon-k ">
                Thread
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                Stats
               </span>
               <span class="crayon-e">
                Avg
               </span>
               <span class="crayon-e">
                Stdev
               </span>
               <span class="crayon-k ">
                Max
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                +
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                -
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                Stdev
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ad570560215-5">
               <span class="crayon-e">
               </span>
               <span class="crayon-i">
                Latency
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                7.36ms
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                37.23ms
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                359.84ms
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                96.43
               </span>
               <span class="crayon-o">
                %
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ad570560215-6">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                Req
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-i">
                Sec
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                2.98k
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                1.03k
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                3.69k
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                84.91
               </span>
               <span class="crayon-o">
                %
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ad570560215-7">
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                16293
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                requests
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                10.08s
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                0.90MB
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                read
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ad570560215-8">
               <span class="crayon-v">
                Requests
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                sec
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                1616.60
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ad570560215-9">
               <span class="crayon-v">
                Transfer
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                sec
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                91.57KB
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0106 seconds] -->
       <p>
       </p>
       <p>
        还不错。
       </p>
       <h2>
        2. 用进程或线程处理多个客户端
       </h2>
       <p>
        既然在 第1章 中因为
        <code>
         accept
        </code>
        和
        <code>
         recv
        </code>
        互相阻塞，服务器才不能同时处理多个请求，那么把它们拆出来不就好了？
       </p>
       <p>
        这是早期大多数服务器使用的办法，具体实现有下面几种。
       </p>
       <h3>
        2.1 CGI 的方式
       </h3>
       <p>
        代码.
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a0bc38ba603143264" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <span class="crayon-language">
           Python
          </span>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          import os
import sys
import socket
import multiprocessing 

response = 'HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-Length: 1\r\n\r\nA'

server = socket.socket()
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(('0.0.0.0', 8080))
server.listen(32)

def handler(client):
        request = client.recv(4096)
        client.send(response)
        client.close()
        sys.exit()

while True:
        client, addr = server.accept()
        process = multiprocessing.Process(target=handler, args=(client,))
        process.daemon = True
        process.start()
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57686a0bc38ba603143264-1">
               1
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ba603143264-2">
               2
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ba603143264-3">
               3
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ba603143264-4">
               4
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ba603143264-5">
               5
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ba603143264-6">
               6
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ba603143264-7">
               7
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ba603143264-8">
               8
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ba603143264-9">
               9
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ba603143264-10">
               10
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ba603143264-11">
               11
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ba603143264-12">
               12
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ba603143264-13">
               13
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ba603143264-14">
               14
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ba603143264-15">
               15
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ba603143264-16">
               16
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ba603143264-17">
               17
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ba603143264-18">
               18
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ba603143264-19">
               19
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ba603143264-20">
               20
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ba603143264-21">
               21
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ba603143264-22">
               22
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ba603143264-23">
               23
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57686a0bc38ba603143264-1">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                os
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ba603143264-2">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                sys
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ba603143264-3">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ba603143264-4">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                multiprocessing
               </span>
               <span class="crayon-h">
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ba603143264-5">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ba603143264-6">
               <span class="crayon-v">
                response
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-s">
                'HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-Length: 1\r\n\r\nA'
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ba603143264-7">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ba603143264-8">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ba603143264-9">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                setsockopt
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                SOL_SOCKET
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                SO_REUSEADDR
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ba603143264-10">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                bind
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                '0.0.0.0'
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                8080
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ba603143264-11">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                listen
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                32
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ba603143264-12">
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ba603143264-13">
               <span class="crayon-r">
                def
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                handler
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ba603143264-14">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                request
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                recv
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                4096
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ba603143264-15">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                send
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                response
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ba603143264-16">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                close
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ba603143264-17">
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                sys
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                exit
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ba603143264-18">
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ba603143264-19">
               <span class="crayon-st">
                while
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                True
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ba603143264-20">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                addr
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                accept
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ba603143264-21">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                process
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                multiprocessing
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                Process
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                target
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-v">
                handler
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                args
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ba603143264-22">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                process
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                daemon
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                True
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ba603143264-23">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                process
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                start
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0187 seconds] -->
       <p>
       </p>
       <p>
        这个方法利用了
        <code>
         UNIX
        </code>
        中子进程会继承父进程所有
        <code>
         fd
        </code>
        的特性。
       </p>
       <p>
        代码开始的部分和 第1章 是一样的，这里的
        <code>
         accept
        </code>
        依然会阻塞，但一旦新的连接建立，我会创建一个新的进程去调用
        <code>
         recv
        </code>
        和
        <code>
         send
        </code>
        （
        <code>
         handler
        </code>
        函数在新的进程中执行）。在主进程中立即再次调用
        <code>
         accept
        </code>
        准备接收下一个客户端连接。
       </p>
       <p>
        CGI 虽然 能处理多个客户端的请求，可是它实在太慢了。打个比方，第1章 的代码相当于一辆汽车在一条公路上行驶，虽然每次只能送一个人，但它可以开得飞快。在 CGI 中变成了无数辆汽车，一旦汽车的数量超过道路的负载就堵车了。
       </p>
       <p>
        用
        <code>
         wrk
        </code>
        测试一下性能
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a0bc38c4102870974" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <span class="crayon-language">
           Python
          </span>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          $ wrk http://127.0.0.1:8080
Running 10s test @ http://127.0.0.1:8080
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    23.01ms    5.14ms  52.45ms   78.82%
    Req/Sec   214.53     35.85   303.00     76.00%
  4296 requests in 10.06s, 243.33KB read
Requests/sec:    427.08
Transfer/sec:     24.19KB
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57686a0bc38c4102870974-1">
               1
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38c4102870974-2">
               2
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38c4102870974-3">
               3
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38c4102870974-4">
               4
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38c4102870974-5">
               5
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38c4102870974-6">
               6
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38c4102870974-7">
               7
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38c4102870974-8">
               8
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38c4102870974-9">
               9
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57686a0bc38c4102870974-1">
               <span class="crayon-sy">
                $
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                wrk
               </span>
               <span class="crayon-v">
                http
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-cn">
                127.0.0.1
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-cn">
                8080
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38c4102870974-2">
               <span class="crayon-i">
                Running
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                10s
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                test
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-sy">
                @
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                http
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-cn">
                127.0.0.1
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-cn">
                8080
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38c4102870974-3">
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                2
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                threads
               </span>
               <span class="crayon-st">
                and
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                10
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                connections
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38c4102870974-4">
               <span class="crayon-e">
               </span>
               <span class="crayon-k ">
                Thread
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                Stats
               </span>
               <span class="crayon-e">
                Avg
               </span>
               <span class="crayon-e">
                Stdev
               </span>
               <span class="crayon-k ">
                Max
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                +
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                -
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                Stdev
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38c4102870974-5">
               <span class="crayon-e">
               </span>
               <span class="crayon-i">
                Latency
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                23.01ms
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                5.14ms
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                52.45ms
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                78.82
               </span>
               <span class="crayon-o">
                %
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38c4102870974-6">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                Req
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-i">
                Sec
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                214.53
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                35.85
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                303.00
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                76.00
               </span>
               <span class="crayon-o">
                %
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38c4102870974-7">
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                4296
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                requests
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                10.06s
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                243.33KB
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                read
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38c4102870974-8">
               <span class="crayon-v">
                Requests
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                sec
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                427.08
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38c4102870974-9">
               <span class="crayon-v">
                Transfer
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                sec
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                24.19KB
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0044 seconds] -->
       <p>
       </p>
       <p>
        实在太慢了。
       </p>
       <p>
        为了解决这个问题，我有个新的办法。
       </p>
       <h3>
        2.2 Thread 线程
       </h3>
       <p>
        先看代码
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a0bc38cb678248785" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <span class="crayon-language">
           Python
          </span>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          import os
import sys
import socket
import threading

response = 'HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-Length: 1\r\n\r\nA'

server = socket.socket()
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(('0.0.0.0', 8080))
server.listen(32)

def handler(client):
        request = client.recv(4096)
        client.send(response)
        client.close()

while True:
        client, clientaddr = server.accept()
        thread = threading.Thread(target=handler, args=(client,))
        thread.daemon = True
        thread.start()
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57686a0bc38cb678248785-1">
               1
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38cb678248785-2">
               2
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38cb678248785-3">
               3
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38cb678248785-4">
               4
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38cb678248785-5">
               5
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38cb678248785-6">
               6
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38cb678248785-7">
               7
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38cb678248785-8">
               8
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38cb678248785-9">
               9
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38cb678248785-10">
               10
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38cb678248785-11">
               11
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38cb678248785-12">
               12
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38cb678248785-13">
               13
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38cb678248785-14">
               14
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38cb678248785-15">
               15
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38cb678248785-16">
               16
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38cb678248785-17">
               17
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38cb678248785-18">
               18
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38cb678248785-19">
               19
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38cb678248785-20">
               20
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38cb678248785-21">
               21
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38cb678248785-22">
               22
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57686a0bc38cb678248785-1">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                os
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38cb678248785-2">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                sys
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38cb678248785-3">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38cb678248785-4">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                threading
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38cb678248785-5">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38cb678248785-6">
               <span class="crayon-v">
                response
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-s">
                'HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-Length: 1\r\n\r\nA'
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38cb678248785-7">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38cb678248785-8">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38cb678248785-9">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                setsockopt
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                SOL_SOCKET
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                SO_REUSEADDR
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38cb678248785-10">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                bind
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                '0.0.0.0'
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                8080
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38cb678248785-11">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                listen
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                32
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38cb678248785-12">
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38cb678248785-13">
               <span class="crayon-r">
                def
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                handler
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38cb678248785-14">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                request
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                recv
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                4096
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38cb678248785-15">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                send
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                response
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38cb678248785-16">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                close
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38cb678248785-17">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38cb678248785-18">
               <span class="crayon-st">
                while
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                True
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38cb678248785-19">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                clientaddr
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                accept
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38cb678248785-20">
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                thread
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                threading
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-k ">
                Thread
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                target
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-v">
                handler
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                args
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38cb678248785-21">
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                thread
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                daemon
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                True
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38cb678248785-22">
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                thread
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                start
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0115 seconds] -->
       <p>
       </p>
       <p>
        代码和 CGI 几乎是一样的，只是把进程换成了线程。既然给汽车太多会堵车，那么给每个人发一辆自行车好了，这样一条路就可以让更多的人通过了。
       </p>
       <p>
        显然这样做是治标不治本啊，自行车多了，不还是会堵车吗。我又想到了新的办法。
       </p>
       <h3>
        2.3 Prefork
       </h3>
       <p>
        代码
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a0bc38d2315407311" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <span class="crayon-language">
           Python
          </span>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          import os
import sys
import socket
import multiprocessing

response = 'HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-Length: 1\r\n\r\nA'

server = socket.socket()
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(('0.0.0.0', 8080))
server.listen(32)

def handler():
        while True:
                client, addr = server.accept()
                request = client.recv(4096)
                client.send(response)
                client.close()
        sys.exit()

processes = []
for i in range(0, 4):
        process = multiprocessing.Process(target=handler, args=())
        process.start()
        processes.append(process)

for process in processes:
        process.join()
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57686a0bc38d2315407311-1">
               1
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38d2315407311-2">
               2
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38d2315407311-3">
               3
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38d2315407311-4">
               4
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38d2315407311-5">
               5
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38d2315407311-6">
               6
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38d2315407311-7">
               7
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38d2315407311-8">
               8
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38d2315407311-9">
               9
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38d2315407311-10">
               10
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38d2315407311-11">
               11
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38d2315407311-12">
               12
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38d2315407311-13">
               13
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38d2315407311-14">
               14
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38d2315407311-15">
               15
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38d2315407311-16">
               16
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38d2315407311-17">
               17
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38d2315407311-18">
               18
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38d2315407311-19">
               19
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38d2315407311-20">
               20
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38d2315407311-21">
               21
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38d2315407311-22">
               22
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38d2315407311-23">
               23
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38d2315407311-24">
               24
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38d2315407311-25">
               25
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38d2315407311-26">
               26
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38d2315407311-27">
               27
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38d2315407311-28">
               28
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57686a0bc38d2315407311-1">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                os
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38d2315407311-2">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                sys
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38d2315407311-3">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38d2315407311-4">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                multiprocessing
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38d2315407311-5">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38d2315407311-6">
               <span class="crayon-v">
                response
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-s">
                'HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-Length: 1\r\n\r\nA'
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38d2315407311-7">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38d2315407311-8">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38d2315407311-9">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                setsockopt
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                SOL_SOCKET
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                SO_REUSEADDR
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38d2315407311-10">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                bind
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                '0.0.0.0'
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                8080
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38d2315407311-11">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                listen
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                32
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38d2315407311-12">
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38d2315407311-13">
               <span class="crayon-r">
                def
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                handler
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38d2315407311-14">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                while
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                True
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38d2315407311-15">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                addr
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                accept
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38d2315407311-16">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                request
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                recv
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                4096
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38d2315407311-17">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                send
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                response
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38d2315407311-18">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                close
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38d2315407311-19">
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                sys
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                exit
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38d2315407311-20">
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38d2315407311-21">
               <span class="crayon-v">
                processes
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-sy">
                ]
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38d2315407311-22">
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-i">
                i
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                range
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                4
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38d2315407311-23">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                process
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                multiprocessing
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                Process
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                target
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-v">
                handler
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                args
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38d2315407311-24">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                process
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                start
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38d2315407311-25">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                processes
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                append
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                process
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38d2315407311-26">
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38d2315407311-27">
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                process
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                processes
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38d2315407311-28">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                process
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                join
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0166 seconds] -->
       <p>
       </p>
       <p>
        聪明的我发现，如果道路只允许固定的汽车同时通过能够保证道路的畅通，而且其它汽车等待的时间比堵车的时间还要短。那我干脆只允许每次只有固定数量的汽车通过好了，如果路上没有位置，就让他等待别的车行驶到终点。如果路上还有空余的位置，就立即通行。这样的设计即能同时处理多个请求，也能保证不会让系统变得很慢。
       </p>
       <p>
        这个设计简直太聪明了。现在有相当多的服务器是这样实现的。
       </p>
       <p>
        因为
        <code>
         UNIX
        </code>
        子进程会继承父进程的所有
        <code>
         fd
        </code>
        。显然
        <code>
         server
        </code>
        变量也不例外，这样只要在子进程中调用
        <code>
         accept
        </code>
        和
        <code>
         recv
        </code>
        就好了（相当于多个 第1章 中的实例），即使阻塞也只会阻塞一个进程。这个方式能够同时处理请求的数量等于子进程的数量。
       </p>
       <p>
        你可能会有一个问题，“如果多个子进程同时阻塞在
        <code>
         accept
        </code>
        上，那么新的连接来了应该唤醒谁？”
       </p>
       <p>
        这个现象叫
        <strong>
         “惊群效应”
        </strong>
        ，在早期
        <code>
         Linux
        </code>
        内核中，每个阻塞在
        <code>
         accept
        </code>
        的子进程都会被唤醒，但只有一个进程能成功建立连接，其它的进程会返回一个错误。新的
        <code>
         Linux
        </code>
        内核已经不存在这个问题，每次只会唤醒一个进程，其它进程继续等待新的连接。
       </p>
       <p>
        这个时候想，如果把
        <code>
         Prefork
        </code>
        中的 “汽车” 换成 “自行车” 会怎样？
       </p>
       <h3>
        2.4 ThreadPool 线程池
       </h3>
       <p>
        ThreadPool 有两个版本，第一个版本在主线程中
        <code>
         accept
        </code>
        子线程中
        <code>
         recv
        </code>
        和
        <code>
         send
        </code>
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a0bc38da205137694" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <span class="crayon-language">
           Python
          </span>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          import os
import sys
import time
import Queue
import socket
import threading

response = 'HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-Length: 1\r\n\r\nA'

server = socket.socket()
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(('0.0.0.0', 8080))
server.listen(32)

def handler(queue):
        while True:
                client  = queue.get()
                request = client.recv(4096)
                client.send(response)
                client.close()
        thread.exit()

queue = Queue.Queue()
threads = []

for i in range(0, 4):
        thread = threading.Thread(target=handler, args=(queue,))
        thread.daemon = True
        thread.start()
        threads.append(thread)

while True:
        client, clientaddr = server.accept()
        queue.put(client)
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57686a0bc38da205137694-1">
               1
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38da205137694-2">
               2
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38da205137694-3">
               3
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38da205137694-4">
               4
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38da205137694-5">
               5
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38da205137694-6">
               6
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38da205137694-7">
               7
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38da205137694-8">
               8
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38da205137694-9">
               9
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38da205137694-10">
               10
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38da205137694-11">
               11
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38da205137694-12">
               12
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38da205137694-13">
               13
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38da205137694-14">
               14
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38da205137694-15">
               15
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38da205137694-16">
               16
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38da205137694-17">
               17
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38da205137694-18">
               18
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38da205137694-19">
               19
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38da205137694-20">
               20
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38da205137694-21">
               21
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38da205137694-22">
               22
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38da205137694-23">
               23
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38da205137694-24">
               24
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38da205137694-25">
               25
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38da205137694-26">
               26
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38da205137694-27">
               27
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38da205137694-28">
               28
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38da205137694-29">
               29
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38da205137694-30">
               30
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38da205137694-31">
               31
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38da205137694-32">
               32
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38da205137694-33">
               33
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38da205137694-34">
               34
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57686a0bc38da205137694-1">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                os
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38da205137694-2">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                sys
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38da205137694-3">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                time
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38da205137694-4">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                Queue
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38da205137694-5">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38da205137694-6">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                threading
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38da205137694-7">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38da205137694-8">
               <span class="crayon-v">
                response
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-s">
                'HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-Length: 1\r\n\r\nA'
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38da205137694-9">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38da205137694-10">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38da205137694-11">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                setsockopt
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                SOL_SOCKET
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                SO_REUSEADDR
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38da205137694-12">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                bind
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                '0.0.0.0'
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                8080
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38da205137694-13">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                listen
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                32
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38da205137694-14">
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38da205137694-15">
               <span class="crayon-r">
                def
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                handler
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-k ">
                queue
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38da205137694-16">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                while
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                True
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38da205137694-17">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                queue
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                get
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38da205137694-18">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                request
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                recv
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                4096
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38da205137694-19">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                send
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                response
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38da205137694-20">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                close
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38da205137694-21">
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                thread
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                exit
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38da205137694-22">
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38da205137694-23">
               <span class="crayon-k ">
                queue
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                Queue
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-k ">
                Queue
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38da205137694-24">
               <span class="crayon-v">
                threads
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-sy">
                ]
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38da205137694-25">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38da205137694-26">
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-i">
                i
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                range
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                4
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38da205137694-27">
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                thread
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                threading
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-k ">
                Thread
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                target
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-v">
                handler
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                args
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-k ">
                queue
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38da205137694-28">
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                thread
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                daemon
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                True
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38da205137694-29">
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                thread
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                start
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38da205137694-30">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                threads
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                append
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-k ">
                thread
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38da205137694-31">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38da205137694-32">
               <span class="crayon-st">
                while
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                True
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38da205137694-33">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                clientaddr
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                accept
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38da205137694-34">
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                queue
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                put
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0158 seconds] -->
       <p>
       </p>
       <p>
        第二个版本，子线程同时
        <code>
         accept
        </code>
        并
        <code>
         recv
        </code>
        和
        <code>
         send
        </code>
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a0bc38e1027106514" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <span class="crayon-language">
           Python
          </span>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          import os
import sys
import time
import socket
import threading

response = 'HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-Length: 1\r\n\r\nA'

server = socket.socket()
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
server.bind(('0.0.0.0', 8080))
server.listen(32)

def handler():
        while True:
                client, clientaddr = server.accept()
                request = client.recv(4096)
                client.send(response)
                client.close()
        thread.exit()

threads = []

for i in range(0, 4):
        thread = threading.Thread(target=handler, args=())
        thread.start()
        threads.append(thread)

for thread in threads:
        thread.join()
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57686a0bc38e1027106514-1">
               1
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38e1027106514-2">
               2
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38e1027106514-3">
               3
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38e1027106514-4">
               4
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38e1027106514-5">
               5
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38e1027106514-6">
               6
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38e1027106514-7">
               7
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38e1027106514-8">
               8
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38e1027106514-9">
               9
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38e1027106514-10">
               10
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38e1027106514-11">
               11
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38e1027106514-12">
               12
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38e1027106514-13">
               13
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38e1027106514-14">
               14
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38e1027106514-15">
               15
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38e1027106514-16">
               16
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38e1027106514-17">
               17
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38e1027106514-18">
               18
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38e1027106514-19">
               19
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38e1027106514-20">
               20
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38e1027106514-21">
               21
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38e1027106514-22">
               22
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38e1027106514-23">
               23
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38e1027106514-24">
               24
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38e1027106514-25">
               25
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38e1027106514-26">
               26
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38e1027106514-27">
               27
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38e1027106514-28">
               28
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38e1027106514-29">
               29
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38e1027106514-30">
               30
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38e1027106514-31">
               31
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57686a0bc38e1027106514-1">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                os
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38e1027106514-2">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                sys
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38e1027106514-3">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                time
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38e1027106514-4">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38e1027106514-5">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                threading
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38e1027106514-6">
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38e1027106514-7">
               <span class="crayon-v">
                response
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-s">
                'HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-Length: 1\r\n\r\nA'
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38e1027106514-8">
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38e1027106514-9">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38e1027106514-10">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                setsockopt
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                SOL_SOCKET
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                SO_REUSEADDR
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38e1027106514-11">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                setsockopt
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                SOL_SOCKET
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                SO_REUSEPORT
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38e1027106514-12">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                bind
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                '0.0.0.0'
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                8080
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38e1027106514-13">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                listen
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                32
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38e1027106514-14">
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38e1027106514-15">
               <span class="crayon-r">
                def
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                handler
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38e1027106514-16">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                while
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                True
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38e1027106514-17">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                clientaddr
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                accept
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38e1027106514-18">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                request
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                recv
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                4096
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38e1027106514-19">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                send
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                response
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38e1027106514-20">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                close
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38e1027106514-21">
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                thread
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                exit
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38e1027106514-22">
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38e1027106514-23">
               <span class="crayon-v">
                threads
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-sy">
                ]
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38e1027106514-24">
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38e1027106514-25">
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-i">
                i
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                range
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                4
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38e1027106514-26">
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                thread
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                threading
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-k ">
                Thread
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                target
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-v">
                handler
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                args
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38e1027106514-27">
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                thread
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                start
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38e1027106514-28">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                threads
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                append
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-k ">
                thread
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38e1027106514-29">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38e1027106514-30">
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                thread
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                threads
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38e1027106514-31">
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                thread
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                join
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0186 seconds] -->
       <p>
       </p>
       <p>
        这个就是 “
        <code>
         Prefork
        </code>
        ” 的线程池版。现在也有相当多的服务器是用这个方法实现的。
       </p>
       <p>
        很难说
        <code>
         Prefork
        </code>
        和
        <code>
         ThreadPool
        </code>
        哪个更好，如果业务相对独立
        <code>
         Prefork
        </code>
        提供了更好的隔离性，进程之间不能访问彼此的内存空间，而且即使有一个子进程甚至主进程崩溃都不会影响正常的子进程。但如果需要共享数据的话，
        <code>
         Prefork
        </code>
        是相当的麻烦。
       </p>
       <p>
        当需要共享大量数据的时候
        <code>
         ThreadPool
        </code>
        用起来会更顺手。但也同时需要注意多线程临界区的加锁等。实现不好可能会变成死锁或者效率太低。
       </p>
       <p>
        在线程池的版本中，第一个
        <code>
         ThreadPool
        </code>
        中的例子在
        <code>
         queue
        </code>
        上会有读写锁（主线程写，子线程读）。不过可以用 Lock Free 算法实现一个精巧的 Ring Buffer 避免这个问题，后边服务器优化的文章再介绍。
       </p>
       <p>
        第二个
        <code>
         ThreadPool
        </code>
        的例子和
        <code>
         prefork
        </code>
        版本几乎是一模一样的。依赖内核的
        <code>
         accept
        </code>
        的实现。
       </p>
       <h3>
        2.5 SO_REUSEPORT Linux 独有的方式
       </h3>
       <p>
        在
        <code>
         Linux 3.9
        </code>
        版的内核中增加了
        <code>
         SO_REUSEPORT
        </code>
        支持。允许多个 socket 绑定同到一个
        <code>
         (ip, port)
        </code>
        ，由内核负责向不同的
        <code>
         socket
        </code>
        分发连接。
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a0bc38e9261017055" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <span class="crayon-language">
           Python
          </span>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          server.setsockopt(socket.SOL_SOCKET, socket. SO_REUSEPORT, 1)
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57686a0bc38e9261017055-1">
               1
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57686a0bc38e9261017055-1">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                setsockopt
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                SOL_SOCKET
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                SO_REUSEPORT
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0041 seconds] -->
       <p>
       </p>
       <p>
        实现相当于把第一章的代码加上上面一行，并且多开几个实例就可以了。这个功能非常简单好用。但注意只在
        <code>
         Linux
        </code>
        下能用，
        <code>
         BSD
        </code>
        上虽然也有
        <code>
         SO_REUSEPORT
        </code>
        但并不能实现内核分发的功能。
       </p>
       <h2>
        3. Nonblocking 非阻塞
       </h2>
       <p>
        不好意思，前面的两章其实只是铺垫。Nonblocking 的服务器，才是现在主流技术。
       </p>
       <p>
        第二章中的 CGI, Thread, Prefork, ThreadPool 的目标只有一个，解决阻塞调用拖慢服务器的问题。费了这么大的功夫，为什么不直接把“阻塞”的函数变成“不阻塞”的呢？
       </p>
       <p>
        上代码
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a0bc38ef862162085" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <span class="crayon-language">
           Python
          </span>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          import os
import fcntl
import socket
import select

response = 'HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-Length: 1\r\n\r\nA'

server = socket.socket()
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(('0.0.0.0', 8080))
server.listen(32)

# set nonblocking
flags = fcntl.fcntl(server.fileno(), fcntl.F_GETFL)
fcntl.fcntl(server.fileno(), fcntl.F_SETFL, flags | os.O_NONBLOCK)

clients = set([])

while True:
        try:
                client, clientaddr = server.accept()
                clients.add(client)
        except Exception as e:
                pass

        for client in clients.copy():
                try:
                        request = client.recv(4096)
                        client.send(response)
                        clients.remove(client)
                        client.close()
                except Exception as e:
                        pass
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57686a0bc38ef862162085-1">
               1
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ef862162085-2">
               2
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ef862162085-3">
               3
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ef862162085-4">
               4
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ef862162085-5">
               5
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ef862162085-6">
               6
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ef862162085-7">
               7
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ef862162085-8">
               8
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ef862162085-9">
               9
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ef862162085-10">
               10
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ef862162085-11">
               11
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ef862162085-12">
               12
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ef862162085-13">
               13
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ef862162085-14">
               14
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ef862162085-15">
               15
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ef862162085-16">
               16
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ef862162085-17">
               17
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ef862162085-18">
               18
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ef862162085-19">
               19
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ef862162085-20">
               20
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ef862162085-21">
               21
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ef862162085-22">
               22
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ef862162085-23">
               23
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ef862162085-24">
               24
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ef862162085-25">
               25
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ef862162085-26">
               26
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ef862162085-27">
               27
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ef862162085-28">
               28
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ef862162085-29">
               29
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ef862162085-30">
               30
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ef862162085-31">
               31
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38ef862162085-32">
               32
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38ef862162085-33">
               33
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57686a0bc38ef862162085-1">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                os
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ef862162085-2">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                fcntl
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ef862162085-3">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ef862162085-4">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                select
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ef862162085-5">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ef862162085-6">
               <span class="crayon-v">
                response
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-s">
                'HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-Length: 1\r\n\r\nA'
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ef862162085-7">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ef862162085-8">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ef862162085-9">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                setsockopt
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                SOL_SOCKET
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                SO_REUSEADDR
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ef862162085-10">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                bind
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                '0.0.0.0'
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                8080
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ef862162085-11">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                listen
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                32
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ef862162085-12">
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ef862162085-13">
               <span class="crayon-c">
                # set nonblocking
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ef862162085-14">
               <span class="crayon-v">
                flags
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                fcntl
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-k ">
                fcntl
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                fileno
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                fcntl
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                F_GETFL
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ef862162085-15">
               <span class="crayon-k ">
                fcntl
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-k ">
                fcntl
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                fileno
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                fcntl
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                F_SETFL
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                flags
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                |
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                os
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                O_NONBLOCK
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ef862162085-16">
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ef862162085-17">
               <span class="crayon-v">
                clients
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                set
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ef862162085-18">
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ef862162085-19">
               <span class="crayon-st">
                while
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                True
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ef862162085-20">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                try
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ef862162085-21">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                clientaddr
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                accept
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ef862162085-22">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                clients
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                add
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ef862162085-23">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                except
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                Exception
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                as
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                e
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ef862162085-24">
               <span class="crayon-h">
               </span>
               <span class="crayon-r">
                pass
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ef862162085-25">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ef862162085-26">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                client
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                clients
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-k ">
                copy
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ef862162085-27">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                try
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ef862162085-28">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                request
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                recv
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                4096
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ef862162085-29">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                send
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                response
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ef862162085-30">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                clients
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                remove
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ef862162085-31">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                close
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38ef862162085-32">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                except
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                Exception
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                as
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                e
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38ef862162085-33">
               <span class="crayon-h">
               </span>
               <span class="crayon-r">
                pass
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0126 seconds] -->
       <p>
       </p>
       <p>
        这次的代码有点儿复杂，我慢慢讲。
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a0bc38f6206827839" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <span class="crayon-language">
           Python
          </span>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          flags = fcntl.fcntl(server.fileno(), fcntl.F_GETFL)
fcntl.fcntl(server.fileno(), fcntl.F_SETFL, flags | os.O_NONBLOCK)
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57686a0bc38f6206827839-1">
               1
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38f6206827839-2">
               2
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57686a0bc38f6206827839-1">
               <span class="crayon-v">
                flags
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                fcntl
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-k ">
                fcntl
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                fileno
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                fcntl
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                F_GETFL
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38f6206827839-2">
               <span class="crayon-k ">
                fcntl
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-k ">
                fcntl
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                fileno
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                fcntl
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                F_SETFL
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                flags
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                |
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                os
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                O_NONBLOCK
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0024 seconds] -->
       <p>
       </p>
       <p>
        这两行代码把
        <code>
         server
        </code>
        设置成非阻塞模式。
       </p>
       <p>
        现在
        <code>
         server
        </code>
        上的
        <code>
         accept
        </code>
        调用就成了非阻塞的了，并且在
        <code>
         accept
        </code>
        成功返回的
        <code>
         client
        </code>
        也会继承
        <code>
         server
        </code>
        的非阻塞特性，所以对
        <code>
         client
        </code>
        的
        <code>
         recv
        </code>
        调用，也是非阻塞的。
       </p>
       <p>
        在非阻塞模式下调用阻塞函数（
        <code>
         accept
        </code>
        和
        <code>
         recv
        </code>
        ），如果并没有可用的数据， 原本应阻塞的函数会直接返回
        <code>
         EAGAIN
        </code>
        或
        <code>
         EWOULDBLOCK
        </code>
        错误，程序可以继续执行，不会被操作系统挂起。
       </p>
       <p>
        <code>
         EAGAIN
        </code>
        和
        <code>
         EWOULDBLOCK
        </code>
        的错误码通常是相同的。但从兼容上考虑，需要两个错误分别判断。
       </p>
       <p>
        在
        <code>
         Python
        </code>
        中是以
        <strong>
         异常
        </strong>
        的形式返回这两个错误的，我用
        <code>
         Exception
        </code>
        就为了捕获这两个错误码，处理方式是什么都不做。
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a0bc38fd676372669" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <span class="crayon-language">
           Python
          </span>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          try:
        client, clientaddr = server.accept()
        clients.add(client)
except Exception as e:
        pass
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57686a0bc38fd676372669-1">
               1
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38fd676372669-2">
               2
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38fd676372669-3">
               3
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc38fd676372669-4">
               4
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc38fd676372669-5">
               5
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57686a0bc38fd676372669-1">
               <span class="crayon-st">
                try
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38fd676372669-2">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                clientaddr
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                accept
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38fd676372669-3">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                clients
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                add
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc38fd676372669-4">
               <span class="crayon-st">
                except
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                Exception
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                as
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                e
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc38fd676372669-5">
               <span class="crayon-h">
               </span>
               <span class="crayon-r">
                pass
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0021 seconds] -->
       <p>
       </p>
       <p>
        这是处理非阻塞的
        <code>
         accept
        </code>
        调用，如果
        <code>
         accept
        </code>
        成功返回，就把返回的
        <code>
         client
        </code>
        放到一个
        <code>
         clients
        </code>
        的数组中，准备下一步接收
        <code>
         client
        </code>
        的数据。如果没有新的连接，
        <code>
         accept
        </code>
        抛出异常，我们直接
        <code>
         pass
        </code>
        到下一步。
       </p>
       <p>
        现在进程不会被操作系统挂起，将立即执行下面的代码。
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a0bc3903119025480" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <span class="crayon-language">
           Python
          </span>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          for client in clients.copy():
        try:
                request = client.recv(4096)
                client.send(response)
                clients.remove(client)
                client.close()
        except Exception as e:
                pass
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57686a0bc3903119025480-1">
               1
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3903119025480-2">
               2
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc3903119025480-3">
               3
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3903119025480-4">
               4
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc3903119025480-5">
               5
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3903119025480-6">
               6
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc3903119025480-7">
               7
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3903119025480-8">
               8
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57686a0bc3903119025480-1">
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                client
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                clients
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-k ">
                copy
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3903119025480-2">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                try
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc3903119025480-3">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                request
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                recv
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                4096
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3903119025480-4">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                send
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                response
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc3903119025480-5">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                clients
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                remove
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3903119025480-6">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                close
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc3903119025480-7">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                except
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                Exception
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                as
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                e
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3903119025480-8">
               <span class="crayon-h">
               </span>
               <span class="crayon-r">
                pass
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0035 seconds] -->
       <p>
       </p>
       <p>
        这段代码并不难理解，
        <code>
         for
        </code>
        循环遍历所有已经建立连接的
        <code>
         client
        </code>
        并
        <strong>
         尝试
        </strong>
        调用
        <code>
         recv
        </code>
        。如果
        <code>
         recv
        </code>
        返回数据，继续调用
        <code>
         send
        </code>
        发送回复，然后把
        <code>
         client
        </code>
        从
        <code>
         clients
        </code>
        中删除并关闭。如果抛出异常就
        <code>
         pass
        </code>
        到下一个
        <code>
         client
        </code>
        。
       </p>
       <p>
        因为
        <code>
         Python
        </code>
        不允许在遍历一个数组的时候修改数组的内容 （
        <code>
         clients.remove(c)
        </code>
        ），所以我用遍历的是
        <code>
         clients.copy()
        </code>
        。
       </p>
       <p>
        实际上，这个例子中的代码是有严重性能问题的，假定以下情况。
       </p>
       <ol>
        <li>
         没有新的连接请求。
        </li>
        <li>
         现在也没有任何连接，
         <code>
          clients
         </code>
         数组是空的。
        </li>
       </ol>
       <p>
        当满足这两情况下的时候，服务器负载为 0 。会发生什么？
       </p>
       <ol>
        <li>
         <code>
          accept
         </code>
         会立即失败进入
         <code>
          for
         </code>
         循环。
        </li>
        <li>
         <code>
          for
         </code>
         循环 0 次立即进入下一次循环。
        </li>
       </ol>
       <p>
        服务器逻辑相当于下面的代码。
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a0bc390a684097566" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <span class="crayon-language">
           Python
          </span>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          while True:
        pass
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57686a0bc390a684097566-1">
               1
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc390a684097566-2">
               2
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57686a0bc390a684097566-1">
               <span class="crayon-st">
                while
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                True
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc390a684097566-2">
               <span class="crayon-h">
               </span>
               <span class="crayon-r">
                pass
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0007 seconds] -->
       <p>
       </p>
       <p>
        进入了一个无意义的循环消耗
        <code>
         CPU
        </code>
        ，这个叫做
        <code>
         busy wait
        </code>
        。这种情况在服务器编程中是不允许出现的。
       </p>
       <p>
        那么如何避免这样的情况发生？方法可能让你大跌眼镜。
       </p>
       <p>
        用 blocking 的方式。我们躲避了半天的 blocking ，又回来了。
       </p>
       <h2>
        4. I/O Multiplexing IO多路复用
       </h2>
       <p>
        这是电子通信的术语。把它用在非阻塞编程上，也是恰到好处。
       </p>
       <h3>
        4.1 Select 的例子
       </h3>
       <p>
        代码
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a0bc3910471427142" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <span class="crayon-language">
           Python
          </span>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          import os
import fcntl
import socket
import select

response = 'HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-Length: 1\r\n\r\nA'

server = socket.socket()
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(('0.0.0.0', 8080))
server.listen(32)

flags = fcntl.fcntl(server.fileno(), fcntl.F_GETFL)
fcntl.fcntl(server.fileno(), fcntl.F_SETFL, flags | os.O_NONBLOCK)

clients = set([])

while True:
        rlist = client.copy()
        rlist.add(server)

        rlist, wlist, xlist = select.select(rlist, [], [], 10)

        for fd in rlist:
                if fd == server:
                        client, clientaddr = server.accept()
                        clients.add(client)
                else:
                        request = fd.recv(4096)
                        fd.send(response)
                        clients.remove(fd)
                        fd.close()
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57686a0bc3910471427142-1">
               1
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3910471427142-2">
               2
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc3910471427142-3">
               3
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3910471427142-4">
               4
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc3910471427142-5">
               5
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3910471427142-6">
               6
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc3910471427142-7">
               7
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3910471427142-8">
               8
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc3910471427142-9">
               9
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3910471427142-10">
               10
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc3910471427142-11">
               11
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3910471427142-12">
               12
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc3910471427142-13">
               13
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3910471427142-14">
               14
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc3910471427142-15">
               15
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3910471427142-16">
               16
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc3910471427142-17">
               17
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3910471427142-18">
               18
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc3910471427142-19">
               19
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3910471427142-20">
               20
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc3910471427142-21">
               21
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3910471427142-22">
               22
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc3910471427142-23">
               23
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3910471427142-24">
               24
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc3910471427142-25">
               25
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3910471427142-26">
               26
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc3910471427142-27">
               27
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3910471427142-28">
               28
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc3910471427142-29">
               29
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3910471427142-30">
               30
              </div>
              <div class="crayon-num" data-line="crayon-57686a0bc3910471427142-31">
               31
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686a0bc3910471427142-32">
               32
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57686a0bc3910471427142-1">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                os
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3910471427142-2">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                fcntl
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc3910471427142-3">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3910471427142-4">
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                select
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc3910471427142-5">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3910471427142-6">
               <span class="crayon-v">
                response
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-s">
                'HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-Length: 1\r\n\r\nA'
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc3910471427142-7">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3910471427142-8">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc3910471427142-9">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                setsockopt
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                SOL_SOCKET
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                socket
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                SO_REUSEADDR
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3910471427142-10">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                bind
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                '0.0.0.0'
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                8080
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc3910471427142-11">
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                listen
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                32
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3910471427142-12">
              </div>
              <div class="crayon-line" id="crayon-57686a0bc3910471427142-13">
               <span class="crayon-v">
                flags
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                fcntl
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-k ">
                fcntl
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                fileno
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                fcntl
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                F_GETFL
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3910471427142-14">
               <span class="crayon-k ">
                fcntl
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-k ">
                fcntl
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                fileno
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                fcntl
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                F_SETFL
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                flags
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                |
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                os
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                O_NONBLOCK
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc3910471427142-15">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3910471427142-16">
               <span class="crayon-v">
                clients
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                set
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc3910471427142-17">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3910471427142-18">
               <span class="crayon-st">
                while
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                True
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc3910471427142-19">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                rlist
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-k ">
                copy
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3910471427142-20">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                rlist
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                add
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc3910471427142-21">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3910471427142-22">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                rlist
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                wlist
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                xlist
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                select
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-k ">
                select
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                rlist
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                10
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc3910471427142-23">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3910471427142-24">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                fd
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                rlist
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc3910471427142-25">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                if
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                fd
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                ==
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3910471427142-26">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                clientaddr
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                server
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                accept
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc3910471427142-27">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                clients
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                add
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                client
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3910471427142-28">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                else
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc3910471427142-29">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                request
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                fd
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                recv
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                4096
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3910471427142-30">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                fd
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                send
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                response
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686a0bc3910471427142-31">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                clients
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                remove
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                fd
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686a0bc3910471427142-32">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                fd
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                close
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0106 seconds] -->
       <p>
       </p>
       <p>
        和第3章中非阻塞的代码很像，不同的是增加了一个
        <code>
         select
        </code>
        的系统调用。现在
        <code>
         server
        </code>
        依然是非阻塞的，但
        <code>
         select
        </code>
        调用是阻塞的，这样就解决了第三章非阻塞面临的
        <code>
         busy wait
        </code>
        的问题。
       </p>
       <p>
        那么
        <code>
         select
        </code>
        是如何工作的呢？
       </p>
       <p>
        在第3章中我说过一个非阻塞的
        <code>
         fd
        </code>
        调用
        <code>
         accept
        </code>
        或
        <code>
         recv
        </code>
        时，如果没有可以返回的数据，会立即返回错误
        <code>
         EAGAIN
        </code>
        或
        <code>
         EWOULDBLOCK
        </code>
        。这也是导至第3章服务器
        <code>
         busy wait
        </code>
        的元凶。
       </p>
       <p>
        但如果把一个非阻塞的
        <code>
         fd
        </code>
        作为参数传到
        <code>
         select
        </code>
        中，在没有数据返回的时候，不会返回错误，而是阻塞在调用
        <code>
         select
        </code>
        的地方。这个阻塞，与第1章和第2章中提到的是一样的。
       </p>
       <p>
        与第1，2两章不同的是，
        <code>
         select
        </code>
        可以同时处理
        <strong>
         多个
        </strong>
        <code>
         fd
        </code>
        。也就是代码中的
        <code>
         rlist
        </code>
        。如果
        <code>
         rlist
        </code>
        里任何一个
        <code>
         fd
        </code>
        可以被
        <code>
         recv
        </code>
        （如果是
        <code>
         server
        </code>
        ，则是
        <code>
         accept
        </code>
        ) ，
        <code>
         select
        </code>
        函数会返回相应的
        <code>
         fd
        </code>
        ，所以在后面的处理中
        <code>
         accept
        </code>
        和
        <code>
         recv
        </code>
        我并没有用
        <code>
         try catch
        </code>
        。
       </p>
       <p>
        不要小瞧这个可以处理多个
        <code>
         fd
        </code>
        的调用，它可帮了我的大忙，它就是我们的“
        <strong>
         多路复用
        </strong>
        ”。
       </p>
       <p>
        下面要介绍一下
        <code>
         select
        </code>
        。
       </p>
       <p>
        <code>
         select
        </code>
        函数有 4 个参数
       </p>
       <ol>
        <li>
         第一个参数是
         <code>
          read list
         </code>
         ，准备将来执行
         <code>
          recv
         </code>
         的
         <code>
          fd
         </code>
         数组。
        </li>
        <li>
         第二个参数是
         <code>
          write list
         </code>
         ， 准备将来执行
         <code>
          send
         </code>
         的
         <code>
          fd
         </code>
         数组。
        </li>
        <li>
         第三个参数是
         <code>
          error list
         </code>
         ，可能会发生错误的
         <code>
          fd
         </code>
         数组。
        </li>
        <li>
         第四个参数是一个超时时间。
        </li>
       </ol>
       <p>
        前三个参数都是
        <code>
         fd
        </code>
        的数组，分为三个我们
        <strong>
         感兴趣的事件
        </strong>
        ，可读(
        <code>
         read
        </code>
        )，可写(
        <code>
         write
        </code>
        ) 和 错误(
        <code>
         error
        </code>
        )。
        <code>
         accept
        </code>
        和
        <code>
         recv
        </code>
        均是可读事件。
       </p>
       <p>
        最后一个参数是在没有任何事件发生的超时时间，在代码中我设置的是 10 秒，如果 10 秒内没有任何新连接或数据，
        <code>
         select
        </code>
        会返回空的事件数组。现在的代码里的超时并没有什么用，但如果要实现
        <code>
         timer
        </code>
        的话，这个参数必不可少。
       </p>
       <p>
        在
        <code>
         C
        </code>
        中的
        <code>
         select
        </code>
        要比
        <code>
         Python
        </code>
        中复杂不少，需要处理比较多的语言和系统的细节。
        <code>
         Python
        </code>
        中的封装就好很多，非常易于我们理解，感谢
        <code>
         Python
        </code>
        。
       </p>
       <p>
        未完待续，下一部分我会介绍
        <code>
         epoll
        </code>
        以及对写事件的处理。如果这一部分有不清楚的，欢迎留言。
       </p>
      </div>
      <div>
       <strong>
        注：转载文章均来自于公开网络，仅供学习使用，不会用于任何商业用途，如果侵犯到原作者的权益，请您与我们联系删除或者授权事宜，联系邮箱：contact@dataunion.org。转载数盟网站文章请注明原文章作者，否则产生的任何版权纠纷与数盟无关。
       </strong>
      </div>
      <!--content_text-->
      <div class="fenxian">
       <!-- JiaThis Button BEGIN -->
       <div class="jiathis_style_32x32">
        <p class="jiathis_button_weixin">
        </p>
        <p class="jiathis_button_tsina">
        </p>
        <p class="jiathis_button_qzone">
        </p>
        <p class="jiathis_button_cqq">
        </p>
        <p class="jiathis_button_tumblr">
        </p>
        <a class="jiathis jiathis_txt jtico jtico_jiathis" href="http://www.jiathis.com/share" target="_blank">
        </a>
        <p class="jiathis_counter_style">
        </p>
       </div>
       <!-- JiaThis Button END -->
      </div>
     </article>
     <!--content-->
     <!--相关文章-->
     <div class="xianguan">
      <div class="xianguantitle">
       相关文章！
      </div>
      <ul class="pic">
       <li>
        <a href="http://dataunion.org/24678.html">
         <img src="http://dataunion.org/wp-content/uploads/2016/06/20140917125452915416-216x200.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/24678.html" rel="bookmark" title="python3中的正则模块">
         python3中的正则模块
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/24675.html">
         <img src="http://dataunion.org/wp-content/uploads/2016/06/t015b337bd75d9ef893-161x200.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/24675.html" rel="bookmark" title="注释是恶魔，请不要再写一行注释">
         注释是恶魔，请不要再写一行注释
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/24660.html">
         <img src="http://dataunion.org/wp-content/uploads/2016/06/u16130037972892789947fm21gp0-300x157.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/24660.html" rel="bookmark" title="如何从Github上轻松安装R包">
         如何从Github上轻松安装R包
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/24654.html">
         <img src="http://dataunion.org/wp-content/uploads/2016/06/df53fac99fc53ba5a90666abcca25e6d_b-267x200.png"/>
        </a>
        <a class="link" href="http://dataunion.org/24654.html" rel="bookmark" title="简单形象又有趣地说说强大的神经网络">
         简单形象又有趣地说说强大的神经网络
        </a>
       </li>
      </ul>
     </div>
     <!--相关文章-->
     <div class="comment" id="comments">
      <!-- You can start editing here. -->
      <!-- If comments are open, but there are no comments. -->
      <div class="title">
       期待你一针见血的评论，Come on！
      </div>
      <div id="respond">
       <p>
        不用想啦，马上
        <a href="http://dataunion.org/wp-login.php?redirect_to=http%3A%2F%2Fdataunion.org%2F22882.html">
         "登录"
        </a>
        发表自已的想法.
       </p>
      </div>
     </div>
     <!-- .nav-single -->
    </div>
    <!--Container End-->
    <aside id="sitebar">
     <div class="sitebar_list2">
      <div class="wptag">
       <span class="tagtitle">
        热门标签+
       </span>
       <div class="tagg">
        <ul class="menu" id="menu-%e5%8f%8b%e6%83%85%e9%93%be%e6%8e%a5">
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1605" id="menu-item-1605">
          <a href="http://taidizh.com/">
           泰迪智慧
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-20884" id="menu-item-20884">
          <a href="http://www.transwarp.cn/">
           星环科技
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-3538" id="menu-item-3538">
          <a href="http://datall.org/">
           珈和遥感
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-20888" id="menu-item-20888">
          <a href="http://www.chinahadoop.cn/">
           小象学院
          </a>
         </li>
        </ul>
       </div>
      </div>
     </div>
     <div class="sitebar_list">
      <div class="textwidget">
       <div align="center">
        <a href="http://study.163.com/course/courseMain.htm?courseId=991022" target="_blank">
         <img src="http://dataunion.org/wp-content/uploads/2016/03/dv.jpg"/>
        </a>
       </div>
      </div>
     </div>
     <div class="sitebar_list">
      <h4 class="sitebar_title">
       文章分类
      </h4>
      <div class="tagcloud">
       <a class="tag-link-44" href="http://dataunion.org/category/industry/demo" style="font-size: 10.204724409449pt;" title="4个话题">
        Demo展示
       </a>
       <a class="tag-link-31" href="http://dataunion.org/category/experts" style="font-size: 15.826771653543pt;" title="52个话题">
        专家团队
       </a>
       <a class="tag-link-870" href="http://dataunion.org/category/tech/ai" style="font-size: 19.795275590551pt;" title="273个话题">
        人工智能
       </a>
       <a class="tag-link-488" href="http://dataunion.org/category/%e5%8a%a0%e5%85%a5%e6%95%b0%e7%9b%9f" style="font-size: 8pt;" title="1个话题">
        加入数盟
       </a>
       <a class="tag-link-869" href="http://dataunion.org/category/tech/viz" style="font-size: 17.204724409449pt;" title="93个话题">
        可视化
       </a>
       <a class="tag-link-30" href="http://dataunion.org/category/partners" style="font-size: 10.645669291339pt;" title="5个话题">
        合作伙伴
       </a>
       <a class="tag-link-889" href="http://dataunion.org/category/parterc" style="font-size: 11.582677165354pt;" title="8个话题">
        合作会议
       </a>
       <a class="tag-link-104" href="http://dataunion.org/category/books" style="font-size: 12.96062992126pt;" title="15个话题">
        图书
       </a>
       <a class="tag-link-220" href="http://dataunion.org/category/tech/base" style="font-size: 19.850393700787pt;" title="281个话题">
        基础架构
       </a>
       <a class="tag-link-219" href="http://dataunion.org/category/tech/analysis" style="font-size: 19.409448818898pt;" title="232个话题">
        数据分析
       </a>
       <a class="tag-link-887" href="http://dataunion.org/category/tech/dm" style="font-size: 13.291338582677pt;" title="17个话题">
        数据挖掘
       </a>
       <a class="tag-link-34" href="http://dataunion.org/category/tech" style="font-size: 20.732283464567pt;" title="404个话题">
        文章
       </a>
       <a class="tag-link-1" href="http://dataunion.org/category/uncategorized" style="font-size: 22pt;" title="693个话题">
        未分类
       </a>
       <a class="tag-link-4" href="http://dataunion.org/category/events" style="font-size: 14.503937007874pt;" title="29个话题">
        活动
       </a>
       <a class="tag-link-890" href="http://dataunion.org/category/tech/%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0" style="font-size: 10.204724409449pt;" title="4个话题">
        深度学习
       </a>
       <a class="tag-link-221" href="http://dataunion.org/category/tech/devl" style="font-size: 18.968503937008pt;" title="193个话题">
        编程语言
       </a>
       <a class="tag-link-888" href="http://dataunion.org/category/career" style="font-size: 15.661417322835pt;" title="48个话题">
        职业规划
       </a>
       <a class="tag-link-5" href="http://dataunion.org/category/jobs" style="font-size: 14.11811023622pt;" title="25个话题">
        职位
       </a>
       <a class="tag-link-871" href="http://dataunion.org/category/industry" style="font-size: 15.716535433071pt;" title="49个话题">
        行业
       </a>
       <a class="tag-link-613" href="http://dataunion.org/category/industry/case" style="font-size: 16.984251968504pt;" title="84个话题">
        行业应用
       </a>
       <a class="tag-link-885" href="http://dataunion.org/category/industry/news" style="font-size: 17.425196850394pt;" title="102个话题">
        行业资讯
       </a>
       <a class="tag-link-10" href="http://dataunion.org/category/training" style="font-size: 14.228346456693pt;" title="26个话题">
        课程
       </a>
       <a class="tag-link-16" href="http://dataunion.org/category/sources" style="font-size: 15.661417322835pt;" title="48个话题">
        资源
       </a>
      </div>
     </div>
     <div class="sitebar_list">
      <h4 class="sitebar_title">
       功能
      </h4>
      <ul>
       <li>
        <a href="http://dataunion.org/wp-login.php?action=register">
         注册
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/wp-login.php">
         登录
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/feed">
         文章
         <abbr title="Really Simple Syndication">
          RSS
         </abbr>
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/comments/feed">
         评论
         <abbr title="Really Simple Syndication">
          RSS
         </abbr>
        </a>
       </li>
       <li>
        <a href="https://cn.wordpress.org/" title="基于WordPress，一个优美、先进的个人信息发布平台。">
         WordPress.org
        </a>
       </li>
      </ul>
     </div>
    </aside>
    <div class="clear">
    </div>
   </div>
   <!--main-->
   ﻿
   <footer id="dibu">
    <div class="about">
     <div class="right">
      <ul class="menu" id="menu-%e5%ba%95%e9%83%a8%e8%8f%9c%e5%8d%95">
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-18024" id="menu-item-18024">
        <a href="http://dataunion.org/category/partners">
         合作伙伴
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20881" id="menu-item-20881">
        <a href="http://dataunion.org/contribute">
         文章投稿
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20872" id="menu-item-20872">
        <a href="http://dataunion.org/category/%e5%8a%a0%e5%85%a5%e6%95%b0%e7%9b%9f">
         加入数盟
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22441" id="menu-item-22441">
        <a href="http://dataunion.org/f-links">
         友情链接
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20874" id="menu-item-20874">
        <a href="http://dataunion.org/aboutus">
         关于数盟
        </a>
       </li>
      </ul>
      <p class="banquan">
       数盟社区        ，
        做最棒的数据科学社区
      </p>
     </div>
     <div class="left">
      <ul class="bottomlist">
       <li>
        <a href="http://weibo.com/DataScientistUnion  " target="_blank" 　title="">
         <img src="http://dataunion.org/wp-content/themes/yzipi/images/weibo.png"/>
        </a>
       </li>
       <li>
        <a class="cd-popup-trigger" href="http://dataunion.org/22882.html#0">
         <img src="http://dataunion.org/wp-content/themes/yzipi/images/weixin.png"/>
        </a>
       </li>
      </ul>
      <div class="cd-popup">
       <div class="cd-popup-container">
        <h1>
         扫描二维码,加微信公众号
        </h1>
        <img src="http://dataunion.org/wp-content/themes/yzipi/images/2014-12-06-1515289049.png"/>
        <a class="cd-popup-close" href="http://dataunion.org/22882.html">
        </a>
       </div>
       <!-- cd-popup-container -->
      </div>
      <!-- cd-popup -->
     </div>
    </div>
    <!--about-->
    <div class="bottom">
     <a href="http://dataunion.org/">
      数盟社区
     </a>
     <a href="http://www.miitbeian.gov.cn/" rel="external nofollow" target="_blank">
      京ICP备14026740号
     </a>
     联系我们：
     <a href="mailto:contact@dataunion.org" target="_blank">
      contact@dataunion.org
     </a>
     <div class="tongji">
     </div>
     <!--bottom-->
     <div class="scroll" id="scroll" style="display:none;">
      ︿
     </div>
    </div>
   </footer>
   <!--dibu-->
  </div>
 </body>
</html>